<?php
 	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 	class MY_Controller extends CI_Controller{
        public $user_post_data = array();
        public $token_expiration = 300;    //配置token的有效时长
        public $offset = 10;
        function __construct() {
            parent::__construct();
            $data = file_get_contents("php://input");
            $data = json_decode($data, true);
            if(!isset($data['user_token']) || empty($data['user_token']['user_id']) || (empty($data['user_token']['token']) && empty($data['user_token']['api_secret']))){
            	echo json_encode(array("code" => 0, "msg" => "请先登录"));
            	exit;
            }

            $this->user_post_data = $data;
            // print_r($_SERVER);exit;
            //用户访问统计
            $today_time = strtotime(date("Y-m-d", time()));
            $today_visit = get_field_by_where("user_visit_statistics", "id, count, visit_url", "user_id='{$data['user_token']['user_id']}' AND last_time > '{$today_time}'");
            $time = time();
            $date_time = date("Y-m-d H:i:s", $time);
            if($today_visit){
                $user_visit_statistics['count'] = $today_visit['count'] + 1;
                $user_visit_statistics['last_time'] = $time;
                $user_visit_statistics['visit_url'] = $today_visit['visit_url'] . '&#&' . $_SERVER['REQUEST_URI'] . "|" . $date_time . "|" . json_encode($this->user_post_data);
                $this->db->update("user_visit_statistics", $user_visit_statistics, array("id" => $today_visit['id']));
            }else{
                $user_info = get_field_by_where("users", "nick,head_img_url,add_time", "id='{$data['user_token']['user_id']}'");
                $user_visit_statistics['user_id'] = $data['user_token']['user_id'];
                $user_visit_statistics['nick'] = $user_info['nick'];
                $user_visit_statistics['head_img_url'] = $user_info['head_img_url'];
                $user_visit_statistics['user_reg_time'] = $user_info['add_time'];
                $user_visit_statistics['count'] = 1;
                $user_visit_statistics['first_time'] = $user_visit_statistics['last_time'] = $time;
                $user_visit_statistics['visit_url'] = $_SERVER['REQUEST_URI'] . "|" . $date_time . "|" . json_encode($this->user_post_data);
                $this->db->insert("user_visit_statistics", $user_visit_statistics);
            }
            
            // print_r($this->user_post_data);
        }
	}